// created by noellynch
// Jun 4, 2009

package com.sceneworker.editors.glslshadereditor.uniformreps;

import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jme.math.Matrix3f;
import com.jme.scene.state.GLSLShaderObjectsState;
import com.jme.util.shader.ShaderVariable;
import com.jme.util.shader.uniformtypes.ShaderVariableMatrix3;
import com.myjavautils.swing.MigLayoutUtils;
import com.sceneworker.editors.glslshadereditor.UniformRepresentations.IUniformRepresentation;

public class Mat3Rep extends BaseUniformRep implements IUniformRepresentation {

	public static final String NAME = "Mat3x3";

	@Override
	public String getNameRep() {
		return NAME;
	}

	public void createUniform(GLSLShaderObjectsState cl_state, String str_name) {
		cl_state.setUniform(str_name, new Matrix3f(), true);
	}

	public Class<?> getUniformClassRep() {
		return ShaderVariableMatrix3.class;
	}

	public ArrayList<JTextField> getUIRep(ShaderVariable cl_v, JPanel cl_parentPanel) {
		ShaderVariableMatrix3		lcl_v = (ShaderVariableMatrix3)cl_v;
		
		lcl_v.matrixBuffer.rewind();
		JTextField		lcl_v1 = MigLayoutUtils.numberText(cl_parentPanel, "00", lcl_v.matrixBuffer.get(), false);
		JTextField		lcl_v2 = MigLayoutUtils.numberText(cl_parentPanel, "01", lcl_v.matrixBuffer.get(), false);
		JTextField		lcl_v3 = MigLayoutUtils.numberText(cl_parentPanel, "02", lcl_v.matrixBuffer.get(), true);
		
		JTextField		lcl_v4 = MigLayoutUtils.numberText(cl_parentPanel, "10", lcl_v.matrixBuffer.get(), false);
		JTextField		lcl_v5 = MigLayoutUtils.numberText(cl_parentPanel, "11", lcl_v.matrixBuffer.get(), false);
		JTextField		lcl_v6 = MigLayoutUtils.numberText(cl_parentPanel, "12", lcl_v.matrixBuffer.get(), true);
		
		JTextField		lcl_v7 = MigLayoutUtils.numberText(cl_parentPanel, "20", lcl_v.matrixBuffer.get(), false);
		JTextField		lcl_v8 = MigLayoutUtils.numberText(cl_parentPanel, "21", lcl_v.matrixBuffer.get(), false);
		JTextField		lcl_v9 = MigLayoutUtils.numberText(cl_parentPanel, "22", lcl_v.matrixBuffer.get(), true);
		
		return uiFields(lcl_v1, lcl_v2, lcl_v3, lcl_v4, lcl_v5, lcl_v6, lcl_v7, lcl_v8, lcl_v9);
	}

	public String getValueRep(ShaderVariable cl_v) {
		ShaderVariableMatrix3	lcl_v = (ShaderVariableMatrix3)cl_v;
		lcl_v.matrixBuffer.rewind();
		return out("00 : " + lcl_v.matrixBuffer.get(), 
				"01 : " + lcl_v.matrixBuffer.get(), 
				"02 : " + lcl_v.matrixBuffer.get() + "\n", 
				
				"10 : " + lcl_v.matrixBuffer.get(),
				"11 : " + lcl_v.matrixBuffer.get(),
				"12 : " + lcl_v.matrixBuffer.get() + "\n",
				
				"20 : " + lcl_v.matrixBuffer.get(),
				"21 : " + lcl_v.matrixBuffer.get(),
				"22 : " + lcl_v.matrixBuffer.get() + "\n",
				
				"row major : " + lcl_v.rowMajor);
	}
	
	public	void	updateFromUI(GLSLShaderObjectsState cl_shaderState, ArrayList<JTextField> cla_values, ShaderVariable cl_v) {
		Matrix3f		lcl_mat = new Matrix3f();
		lcl_mat.m00 = Float.parseFloat(cla_values.get(0).getText());
		lcl_mat.m01 = Float.parseFloat(cla_values.get(1).getText());
		lcl_mat.m02 = Float.parseFloat(cla_values.get(2).getText());
		
		lcl_mat.m10 = Float.parseFloat(cla_values.get(3).getText());
		lcl_mat.m11 = Float.parseFloat(cla_values.get(4).getText());
		lcl_mat.m12 = Float.parseFloat(cla_values.get(5).getText());
		
		lcl_mat.m20 = Float.parseFloat(cla_values.get(6).getText());
		lcl_mat.m21 = Float.parseFloat(cla_values.get(7).getText());
		lcl_mat.m22 = Float.parseFloat(cla_values.get(8).getText());
		
		cl_shaderState.setUniform(cl_v.name, lcl_mat, true);
	}

}
